package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.adapter.FragmentAdapter;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.EdinburghOrderTypeFragment;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.RxUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.meitianhui.convenitenbuy.R.id.viewPager;

/**
 * Created by Administrator on 2017/11/14.
 */

public class HomeOrderActivity extends BaseToolbarActivity {

    private String[] mTitles = new String[]{"全部", "待付款", "待返款", "已返款", "已取消"};
    private ImageView mIvBack, mIvMenu;
    private TextView mTvTitle;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentAdapter adapter;
    private List<String> mTitleList = new ArrayList<>();
    private List<Fragment> mFragments;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        // mIvBack = getViewById(R.id.toolbar_back_all);
        mIvMenu = getViewById(R.id.toolbar_iv_menu);
        RxUtils.bindEvents(mIvMenu, this);
        mTvTitle = getViewById(R.id.toolbar_title);
        mTabLayout = getViewById(R.id.tabLayout);
        mViewPager = getViewById(viewPager);
        initData();
    }

    @Override
    protected int setView() {
        return R.layout.activity_home_order;
    }

    private void initData() {
        isEnable(false);
        mTvTitle.setText("订单");
        // mIvBack.setVisibility(View.GONE);
        mIvMenu.setVisibility(View.VISIBLE);
        mIvMenu.setBackgroundResource(R.mipmap.icon_title_message);
        Collections.addAll(mTitleList, mTitles);
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitleList.size(); i++) {
            Bundle bundle = new Bundle();
            if (i == 0) {
                bundle.putString("status", "");
            } else if (i == 1) {        //待确认
                bundle.putString("status", "processing");
            } else if (i == 2) {        //待返款
                bundle.putString("status", "confirmed");
            } else if (i == 3) {        //已返款
                bundle.putString("status", "closed");
            } else if (i == 4) {        //已取消
                bundle.putString("status", "cancelled");
            }
            EdinburghOrderTypeFragment fragment = new EdinburghOrderTypeFragment();
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }
        adapter = new FragmentAdapter(getSupportFragmentManager(), mFragments, mTitleList);
        //给ViewPager设置适配器
        mViewPager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorHeight(0);
        mViewPager.setOffscreenPageLimit(3);
        try {
            for (int i = 0; i < mTitleList.size(); i++) {
                //获得到对应位置的Tab
                TabLayout.Tab itemTab = mTabLayout.getTabAt(i);
                if (itemTab != null) {
                    //设置自定义的标题
                    itemTab.setCustomView(R.layout.item_tab);
                    TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                    textView.setText(mTitleList.get(i));
                }
            }
            mTabLayout.getTabAt(0).getCustomView().setSelected(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    @Override
    public void call(View v) {
        switch (v.getId()) {
            case R.id.toolbar_iv_menu:  //消息
                JumpUtil.jumpToChatActivity(this);
                break;
        }
    }
    */

    @Override
    protected void clickMenu() {
        JumpUtil.jumpToChatActivity(this);
    }

    @Override
    protected void onRefreshData() {
    }

}
